﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Main_Utilities_UserGroups : System.Web.UI.Page
{
    Datautility dut = new Datautility();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["userid"] == null)
        {
            //ScriptManager.RegisterStartupScript(Page, this.GetType(), "redirect", "window.open('../../login.aspx','_parent');", true);
            Response.Redirect("~/SessionExpired.aspx");
        }
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    protected void Insert_Click(object sender, EventArgs e)
    {
        try
        {
            string group_name = TxtGroupName.Text;
            string sql = "select GroupId from UserGroup where GroupName = '" + group_name + "'";
            DataTable DT = dut.getdata(sql);
            if (DT.Rows.Count > 0)
            {
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "Group already exists! ";
            }
            else
            {
                SqlParameter[] parm ={
                                  new SqlParameter("@groupname", TxtGroupName.Text.Trim()),
                                  new SqlParameter("@GroupDesc", txtGroupDescription.Text.Trim()),
                                  new SqlParameter("@CName",Session["username"].ToString()),    
                               
                             };
                dut.executeprocedure("usp_insert_usergroup", parm);
                TxtGroupName.Text = "";
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Group created Successfully...";
                BindGrid();
            }
        }
        catch (Exception ex)
        {
            lblMsg.Attributes.Remove("class");
            lblMsg.Attributes.Add("class", "false");
            lblMsg.Text = ex.Message;
        }
    }
    protected void GrdDisplay_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GrdDisplay.PageIndex = e.NewPageIndex;
        BindGrid();
    }
    protected void GrdDisplay_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GrdDisplay.EditIndex = -1;
        BindGrid();
    }
    protected void GridDisplay_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GrdDisplay.EditIndex = e.NewEditIndex;
        BindGrid();
    }
    protected void GrdDisplay_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox Txtname = (TextBox)GrdDisplay.Rows[e.RowIndex].Cells[1].Controls[0];
        TextBox Txtdesc = (TextBox)GrdDisplay.Rows[e.RowIndex].Cells[2].Controls[0];
        if (Txtname.Text.Trim() != string.Empty)
        {
            string key = GrdDisplay.DataKeys[e.RowIndex].Value.ToString();
            string sql = "select GroupId from UserGroup where GroupName =  '" + Txtname.Text + "' and GroupId !=" + key;
            DataTable DT = dut.getdata(sql);
            if (DT.Rows.Count > 0)
            {
                lblMsg.Attributes.Add("class", "false");
                lblMsg.Text = "Group already exists! ";

            }
            else
            {
                SqlParameter[] parm ={
                               new SqlParameter("@groupname",Txtname.Text.Trim()),
                               new SqlParameter("@GroupDesc",Txtdesc.Text.Trim()),
                               new SqlParameter("@groupid",key),
                               new SqlParameter("@EName",Session["username"].ToString())  
                             };
                dut.executeprocedure("usp_update_usergroup", parm);
                lblMsg.Attributes.Remove("class");
                lblMsg.Attributes.Add("class", "true");
                lblMsg.Text = "Group details updated successfully...";
                GrdDisplay.EditIndex = -1;
                BindGrid();
            }
        }
    }
    private void BindGrid()
    {
        DataTable DT = dut.getdatausingprocedure("usp_select_all_usergroup");
        GrdDisplay.DataSource = DT;
        GrdDisplay.DataBind();
    }
}
